package com.personal.book.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.personal.book.entity.Orders;
import com.personal.book.entity.OrdersDetails;
import com.personal.book.entity.ShoppingCar;
import com.personal.book.service.BookService;
import com.personal.book.service.OrdersDetailsService;
import com.personal.book.service.OrdersService;

import com.personal.book.service.impl.BookServiceImpl;
import com.personal.book.service.impl.OrdersDetailsServiceImpl;
import com.personal.book.service.impl.OrdersServiceImpl;

/**
 * Servlet implementation class ShoppingCarServlet
 */
@WebServlet("/ShoppingCarServlet")
public class ShoppingCarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ShoppingCarServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		response.setContentType("application/json;charset=utf-8");
		OrdersService service = new OrdersServiceImpl();
		OrdersDetailsService detailsService = new OrdersDetailsServiceImpl();
		BookService bookService = new BookServiceImpl();
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		if ("add".equals(op)) {
			List<ShoppingCar> list = new ArrayList<ShoppingCar>();
			double allprice = 0;
			int num = 0;
			Date date = new Date();
			int orderId = (int) date.getTime();
			for (int i = 1; i < 101; i++) {
				String bookId = request.getParameter("sc_bookId_" + i);
				String bookUrl = request.getParameter("sc_bookUrl_" + i);
				String bookName = request.getParameter("chr_item_" + i);
				String count = request.getParameter("sc_count_" + i);
				String quantity = request.getParameter("quantity_" + i);
				String price = request.getParameter("amount_" + i);
				if (bookId != null && bookUrl != null && bookName != null && count != null && quantity != null
						&& price != null) {
					ShoppingCar car = new ShoppingCar(Integer.parseInt(bookId), bookUrl, bookName,
							Integer.parseInt(count), Integer.parseInt(quantity), Double.parseDouble(price));
					double totalprice = Integer.parseInt(quantity) * Double.parseDouble(price);
					OrdersDetails ordersDetails = new OrdersDetails(orderId, Integer.parseInt(bookId), bookName,
							Integer.parseInt(quantity), totalprice);
					allprice += totalprice;
					bookService.updateBookCount(Integer.parseInt(bookId), Integer.parseInt(quantity));
					detailsService.addOrdersDetailsInfo(ordersDetails);
					list.add(car);
				} else {
					num = i - 1;
					break;
				}
			}

			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
			String userName = (String) request.getSession().getAttribute("userName");
			Orders order = new Orders(orderId, df.format(new Date()), userName, allprice);
			service.AddOrders(order);
			request.setAttribute("orderId", orderId);
			request.setAttribute("num", num);
			request.setAttribute("allprice", allprice);
			request.setAttribute("list", list);
			request.getRequestDispatcher("shoppingcar.jsp").forward(request, response);

		}else if ("addother".equals(op)) {
			String userName = (String) request.getSession().getAttribute("userName");
			int orderId = Integer.parseInt(request.getParameter("orderId")); 
			String addr = request.getParameter("addr");
			String tel = request.getParameter("tel");
			detailsService.addArdsTel(orderId, addr, tel);
			List<Orders> list = service.getOrdersByName(userName);
			request.setAttribute("list", list);
			request.getRequestDispatcher("orders.jsp").forward(request, response);
			
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
